package com.zhuss.order.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.order.order.OverOrderTimeBo;
import com.zhuss.bean.unit.order.TrainGroupBean;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.exception.DzcxException;
import com.zhuss.order.db.dao.ext.OrderDao;
import com.zhuss.order.db.dao.ext.OrderItemDao;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.util.DateUtil;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.ListBean;
import com.zhuss.util.OrderUseMill;
/**
 * 
 * @author zhushanshuo
 * @since 2020.03.09 13:10
 *
 */

@Component("orderOverTime")
public class OrderOverTime extends AbstractProcessor<Object, OverOrderTimeBo> implements Processor<Object, OverOrderTimeBo> {
	
	@Autowired
	private TrainDao trainDao;
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private OrderItemDao orderItemDao;
	
	@Autowired
	private OrderCheck orderCheck;
	
	@Override
	public void check(OverOrderTimeBo bo) throws DzcxException {
	}
	
	@Override
	public Object process(OverOrderTimeBo bo) throws DzcxException {
		Date start = DateUtil.toDateOnly(new Date(0l));
		Date end = new Date(System.currentTimeMillis() - OrderUseMill.USE_MILL * 2);
		List<Long> orderIdList = orderDao.listByStartAndEnd(start, end, new ListBean<Integer>().build(OrderStatus.未支付.key()).build(OrderStatus.已提交支付.key()).getList());
		if (orderIdList != null && !orderIdList.isEmpty()) {
			Date now = new Date();
			orderDao.cancelBatch(orderIdList, OrderStatus.定时任务补偿过期.key(), OrderStatus.定时任务补偿过期.value(), 0l, now);
			orderItemDao.cancelBatch(orderIdList, OrderStatus.定时任务补偿过期.key(), OrderStatus.定时任务补偿过期.value(), 0l, now);
			List<TrainGroupBean> trainList = orderItemDao.listTrain(orderIdList);
			trainDao.unused(trainList);
		}
		bo.setOrderIdList(orderIdList);
		return null;
	}

	@Override
	public Object log(OverOrderTimeBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.订单.key());
    	record.setAction(LogActionEnum.定时任务补偿过期.key());
    	record.setContent(JsonUtil.toJson(param.getOrderIdList()));
    	record.setCreatorId(0l);
    	record.setCreator(LogActionEnum.定时任务补偿过期.value());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}


}
